﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLBH.DataModel;
using QLBH.Model.Controller;
using QLBH.StandardUI;
using QLBH.Category;
using QLBH.ModelInterface;
using QLBH.Base;

namespace QLBH.CongNo
{
    public partial class GhiNoDlg : Form
    {
        private ISoNoModel model;

        public ISoNoModel Model
        {
            get { return model; }
            set { model = value; }
        }
        private IKhachHangModel currentKH;
        private IQLNCCModel currentNCC;
        private eLoaiNo loaiNo;

        public eLoaiNo LoaiNo
        {            
            set 
            { 
                loaiNo = value; 
                switch(loaiNo)
                {
                    case eLoaiNo.LN_KHNO:
                        rbtnKHNo.Checked = true;
                        break;
                    case eLoaiNo.LN_NCCNO:
                        rbtnNCCNo.Checked = true;
                        break;
                    case eLoaiNo.LN_NOKH:
                        rbtnNoKH.Checked = true;
                        break;
                    case eLoaiNo.LN_NONCC:
                        rbtnNoNCC.Checked = true;
                        break;
                }
            }
        }
        public GhiNoDlg()
        {
            InitializeComponent();

            model = ModelFactory.CongNoModelFactory.GetSoNoModelInstance();
        }

        private void GhiNoDlg_Load(object sender, EventArgs e)
        {            
            LoadDSLyDo();
        }        

        private void LoadDSLyDo()
        {
            try
            {
                List<LYDO> lydos = LyDoController.GetInstance().GetAll().ToList();
                cmbLyDo.DataSource = lydos;
                cmbLyDo.DisplayMember = "TEN";
                cmbLyDo.ValueMember = "ID";
            }
            catch (System.Exception ex)
            {
                ShowMessage.Instance.ShowError(ex.Message);
            } 
        }         

        private void lbAddLyDo_Click(object sender, EventArgs e)
        {
            AddLyDoDlg addNewLyDoDlg = new AddLyDoDlg();
            if (addNewLyDoDlg.ShowDialog() == DialogResult.OK)
            {
                cmbLyDo.DataSource = null;
                LoadDSLyDo();
                cmbLyDo.SelectedValue = addNewLyDoDlg.Model.ID;
            }
        }        

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (CheckInputData())
            {
                // make data to model
                if (rbtnKHNo.Checked || rbtnNoKH.Checked)
                {
                    model.KHID = currentKH.KHID;
                    model.TENKH = currentKH.TENKH;
                    model.NCCID = Guid.Empty;
                    model.TENNCC = null;
                }
                else if (rbtnNCCNo.Checked || rbtnNoNCC.Checked)
                {
                    model.KHID = Guid.Empty;
                    model.TENKH = null;
                    model.NCCID = currentNCC.NCCID;
                    model.TENNCC = currentNCC.TENNCC;
                }
                if (rbtnKHNo.Checked || rbtnNCCNo.Checked)
                {
                    model.LOAINO = true;
                }
                else if (rbtnNoKH.Checked || rbtnNoNCC.Checked)
                {
                    model.LOAINO = false;
                }

                model.LYDOID = (cmbLyDo.SelectedItem as LYDO).ID;
                model.NGAYCAPNHAT = DateTime.Now;
                model.NGAYTAO = DateTime.Now;
                model.SOTIEN = decimal.Parse(tbxSoTien.Text);
                model.TENLYDO = (cmbLyDo.SelectedItem as LYDO).TEN;
                model.USERID = AccountHandler.GetCurrentUserLogin().NVID;
                model.USERNAME = AccountHandler.GetCurrentUserLogin().TENNV;

                model.SONOID = Guid.NewGuid();
                //save data

                SONO entity = model.ToEntity();

                try
                {
                    entity = SoNoController.GetInstance().Add(entity);

                    model.InitFromEntity(entity);

                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                catch (System.Exception ex)
                {
                    ShowMessage.Instance.ShowError(ex.Message);
                }                
            }

        }

        private bool CheckInputData()
        {
            if (currentKH==null && currentNCC==null)
            {
                ShowMessage.Instance.ShowError("Bạn chưa chọn thông tin khách hàng hoặc NCC");
                return false;
            }
            try
            {
                DataInputHelper.CheckDecimalData(tbxSoTien.Text, "Số tiền");
            }
            catch (System.Exception ex)
            {
                ShowMessage.Instance.ShowError(ex.Message);
                tbxSoTien.SelectAll();
                tbxSoTien.Focus();
                return false;
            }                                                
            return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void GhiNoDlg_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode==Keys.Escape)
            {
                btnCancel_Click(btnCancel, null);
            }
            else if (e.Control && e.KeyCode==Keys.S)
            {
                btnSave_Click(btnSave, null);
            }
        }

        private void linklbThongTin_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (rbtnKHNo.Checked || rbtnNoKH.Checked)
            {
                FrmSelectCustomer selKhachHangDlg = new FrmSelectCustomer();
                if (selKhachHangDlg.ShowDialog() == DialogResult.OK)
                {
                    currentKH = selKhachHangDlg.SelectedKH;
                    UpdateTTKHToForm();
                }
            }
            else
            {
                frmSelectProviderDlg selectProviderDlg = new frmSelectProviderDlg();
                if (selectProviderDlg.ShowDialog()==DialogResult.OK)
                {
                    currentNCC = selectProviderDlg.SelectedNCC;
                    UpdateNCCToForm();
                }
            }
        }
        private void UpdateNCCToForm()
        {
            tbxHoTen.Text = currentNCC.TENNCC;
            tbxEmail.Text = currentNCC.EMAIL;
            tbxSDT.Text = currentNCC.DIENTHOAI;
            rtbxDiachi.Text = currentNCC.DIACHI;
        }
        private void UpdateTTKHToForm()
        {
            tbxHoTen.Text = currentKH.TENKH;
            tbxEmail.Text = currentKH.EMAIL;
            tbxSDT.Text = currentKH.DIENTHOAI;
            rtbxDiachi.Text = currentKH.DIACHI;
        }
        private void ClearThongTin()
        {
            tbxHoTen.Text = "";
            tbxEmail.Text = "";
            tbxSDT.Text = "";
            rtbxDiachi.Text = "";
        }

        private void LoaiNoRButton_CheckedChanged(object sender, EventArgs e)
        {
            if (((RadioButton)sender).Checked)
            {
                if (rbtnNoKH.Checked || rbtnKHNo.Checked)
                {
                    linklbThongTin.Text = "Thông tin KH";
                    ClearThongTin();
                    currentKH = null;
                }
                else if (rbtnNoNCC.Checked || rbtnNCCNo.Checked)
                {
                    linklbThongTin.Text = "Thông tin NCC";
                    ClearThongTin();
                    currentNCC = null;
                }
            }
        }

    }
}
